package city.spring.modules.material.service.impl;

import city.spring.modules.material.entity.MaterialInventoryEntity;
import city.spring.modules.material.repository.MaterialInventoryRepository;
import city.spring.modules.material.repository.MaterialinventoryEmployeeExtRepository;
import city.spring.modules.material.repository.MaterialinventoryMaterialExtRepository;
import city.spring.modules.material.service.MaterialInventoryService;
import city.spring.modules.material.service.MaterialSettingService;
import city.spring.modules.material.vo.MaterialinventoryMaterialVO;
import city.spring.modules.material.vo.MaterialinventoryUserVO;
import city.spring.modules.material.vo.MaterialinventoryVO;
import city.spring.modules.system.entity.UserEntity;
import city.spring.modules.system.service.UserService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.NonNull;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Service：库存盘点
 *
 * @author 谢金婷
 * @date 2020-06-11 17:29:42
 */
@CacheConfig(cacheNames = {MaterialInventoryServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Throwable.class)
@Service
public class MaterialInventoryServiceImpl extends ServiceImpl<MaterialInventoryRepository, MaterialInventoryEntity> implements MaterialInventoryService {

    private final MaterialSettingService materialSettingService;
    private final UserService userService;
    private final MaterialInventoryRepository inventoryRepository;
    private final MaterialinventoryEmployeeExtRepository materialinventoryEmployeeExtRepository;
    private final MaterialinventoryMaterialExtRepository materialinventoryMaterialExtRepository;


    public MaterialInventoryServiceImpl(MaterialSettingService materialSettingService, UserService userService, MaterialInventoryRepository inventoryRepository, MaterialinventoryEmployeeExtRepository materialinventoryEmployeeExtRepository, MaterialinventoryMaterialExtRepository materialinventoryMaterialExtRepository) {
        this.materialSettingService = materialSettingService;
        this.userService = userService;
        this.inventoryRepository = inventoryRepository;
        this.materialinventoryEmployeeExtRepository = materialinventoryEmployeeExtRepository;
        this.materialinventoryMaterialExtRepository = materialinventoryMaterialExtRepository;
    }

    @Override
    public void saveInventory(MaterialInventoryEntity entity, @NonNull Principal user) {
//
//        MaterialSettingEntity materialSettingEntity = materialSettingService.getmaterialById(String.valueOf(entity.getMaterialId()));
//        System.out.println(materialSettingEntity);
//
        //设置盘点人员为当前登录用户
        UserEntity userEntity = userService.getUserByIdOrAccount(user.getName());
        entity.setFounder(userEntity.getId());
        entity.setRecordDate(new Date());

        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateInventory(MaterialInventoryEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteInventory(String primaryKey) {
        removeById(primaryKey);
        // 删除包括的盘点 关联用户表的信息
        List<MaterialinventoryUserVO> users = materialinventoryEmployeeExtRepository.getInventoryUsers(primaryKey);
        System.out.println("users" + users);
        if (!users.isEmpty()) {
            List<String> userIds = users.stream().map(MaterialinventoryUserVO::getId).collect(Collectors.toList());
            materialinventoryEmployeeExtRepository.deleteBatchIds(userIds);
        }
        // 删除包括的盘点 关联物资表的信息
        List<MaterialinventoryMaterialVO> materials = materialinventoryMaterialExtRepository.getInventoryMaterials(primaryKey);
        System.out.println("materials" + materials);
        if (!materials.isEmpty()) {
            List<String> materialIds = materials.stream().map(MaterialinventoryMaterialVO::getId).collect(Collectors.toList());
            materialinventoryMaterialExtRepository.deleteBatchIds(materialIds);
        }
    }

    @Override
    public void deleteInventory(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public IPage<MaterialInventoryEntity> getInventoryOrder(Pageable pageable) {
        IPage<MaterialInventoryEntity> page = MyBatisUtils.toPage(pageable);
        //ORDER BY storehouse_id DESC, category DESC
        return inventoryRepository.getInventoryOrder(page, lambdaQuery().getWrapper());
    }

    @Override
    public MaterialInventoryEntity getById(String id) {
        return inventoryRepository.getById(id);
    }

    @Override
    public MaterialinventoryVO getMaterialinventoryById(String id) {
        MaterialInventoryEntity inventoryEntity = inventoryRepository.getById(id);
        System.out.println("inventoryEntity" + inventoryEntity);
        MaterialinventoryVO inventoryVO = new MaterialinventoryVO(inventoryEntity);

        // 加载盘点物资列表
        inventoryVO.setMaterials(materialinventoryMaterialExtRepository.getInventoryMaterials(id));
        //加载盘点人员列表
        inventoryVO.setUsers(materialinventoryEmployeeExtRepository.getInventoryUsers(id));

        return inventoryVO;
    }

    @Override
    public IPage<MaterialinventoryVO> listPage(Page<? extends MaterialInventoryEntity> page, Wrapper<? extends MaterialInventoryEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }
}